﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace OMSService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
    public class Service1 : IService1
    {
        #region Item
        public bool CreateItem(ItemModel item)
        {
            return ItemManager.Save(item);
        }

        public List<ItemModel> GetAllItems()
        {
            return ItemManager.GetAll();
        }

        public ItemModel GetItemById(int id)
        {
            return ItemManager.GetById(id);
        }

        public bool EditItem(ItemModel itemModel)
        {
            //TODO: ErrorHandling
            return ItemManager.Update(itemModel);
        }

        public bool DeleteItem(int id)
        {
            return ItemManager.Delete(id);
        }

        public List<ItemModel> FilterByName(string itemName)
        {
            return ItemManager.FilterByName(itemName);
        }

        public List<ItemModel> FilterByDescription(string itemDescr)
        {
            return ItemManager.FilterByDescription(itemDescr);
        }
        #endregion

        #region User
        public bool CreateUser(UserModel userModel)
        {
            return UserManager.Save(userModel);
        }

        public List<UserModel> GetAllUsers()
        {
            return UserManager.GetAll();
        }

        public UserModel GetUserById(int id)
        {
            return UserManager.GetById(id);
        }

        public UserModel GetUserByUserName(string userName)
        {
            return UserManager.Filter("username", "equal", userName)[0];
        }

        public bool EditUser(UserModel userModel)
        {
            return UserManager.Update(userModel);
        }

        public bool DeleteUser(int id)
        {
            return UserManager.Delete(id);
        }

        public int CheckUserPass(string userName, string password)
        {
            return UserManager.CheckPass(userName, password);
        }

        #region Filters

        public List<UserModel> FilterUsers(string field, string condition, string pattern)
        {
            return UserManager.Filter(field, condition, pattern);
        }

        #endregion

        #endregion

        #region CreditCard
        public void CreateCreditCard(CreditCardModel cardModel)
        {
            CreditCardManager.Save(cardModel);
        }

        public List<CreditCardModel> GetAllCreditCards()
        {
            return CreditCardManager.GetAll();
        }

        public CreditCardModel GetCreditCardById(int id)
        {
            return CreditCardManager.GetById(id);
        }

        public void EditCreditCard(CreditCardModel cardModel)
        {
            CreditCardManager.Update(cardModel);
        }

        public void DeleteCreditCard(int id)
        {
            CreditCardManager.Delete(id);
        }

        #endregion

        #region OrderItem
        public bool CreateOrderItem(OrderItemModel item)
        {
            return OrderItemManager.Save(item);
        }

        public List<OrderItemModel> GetAllOrderItems()
        {
            return OrderItemManager.GetAll();
        }

        public OrderItemModel GetOrderItemById(int id)
        {
            return OrderItemManager.GetById(id);
        }

        public bool EditOrderItem(OrderItemModel itemModel)
        {
            //TODO: ErrorHandling
            return OrderItemManager.Update(itemModel);
        }

        public bool DeleteOrderItem(int id)
        {
            return OrderItemManager.Delete(id);
        }
        #endregion

        #region Order
        public int CreateOrder(OrderModel order)
        {
            return OrderManager.Save(order);
        }

        public OrderModel GetOrderById(int id)
        {
            return OrderManager.GetById(id);
        }

        public bool EditOrder(OrderModel orderModel)
        {
            //TODO: ErrorHandling
            return OrderManager.Update(orderModel);
        }

        public bool DeleteOrder(int id)
        {
            return OrderManager.Delete(id);
        }

        public CreditCardModel GetCardByOrderId(int id)
        {
            return OrderManager.GetCreditCard(id);
        }

        public bool AddOrderItemToOrder(int orderId, OrderItemModel orderItemModel)
        {
            return OrderManager.AddOrderItem(orderId, orderItemModel);
        }

        public List<OrderModel> GetAllOrders()
        {
            return OrderManager.GetAllOrders();
        }

        public List<OrderModel> GetOrdersByCustomer(int userId)
        {
            return OrderManager.GetCustomerOrders(userId);
        }

        #region Filters

        public List<OrderModel> OrderFilter(string field, string pattern)
        {
            return OrderManager.Filter(field, pattern);
        }

        #endregion
        #endregion

        #region Helpers
        
        public bool CreateSampleData()
        {
            try
            {
                Helpers.CreateSampleData();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }
        #endregion Helpers
    }
}
